<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="infoId">
        <el-input
          v-model="queryParams.infoId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择退款状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.return_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="refundList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退货编号" align="center" prop="refundId" />
      <el-table-column label="订单号" align="center" prop="infoId" />
      <el-table-column label="商品" align="center">
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.saleDetail.imgUrl"
            :width="50"
            :height="50"
          ></image-preview>
          {{ scope.row.saleDetail.skuName }}
        </template>
      </el-table-column>
      <el-table-column label="商品数量" align="center">
        <template slot-scope="scope">
          {{ scope.row.saleDetail.quantity }}
        </template>
      </el-table-column>
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="退款原因" align="center" prop="reason" />
      <el-table-column label="退款状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.return_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" />
      <el-table-column
        label="退货时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '1'"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleAccept(scope.row)"
            v-hasPermi="['sale:refund:edit']"
            >同意</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listRefund, updateRefund } from "@/api/sale/refund";

export default {
  name: "Refund",
  dicts: ["return_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退款表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        infoId: [
          { required: true, message: "订单号不能为空", trigger: "blur" },
        ],
        detailId: [
          { required: true, message: "订单详情号不能为空", trigger: "blur" },
        ],
        refundAmount: [
          { required: true, message: "退款金额不能为空", trigger: "blur" },
        ],
        reason: [
          { required: true, message: "退款原因不能为空", trigger: "blur" },
        ],
      },
      readFlag: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退款列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then((response) => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        refundId: null,
        infoId: null,
        detailId: null,
        refundAmount: null,
        reason: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.refundId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAccept(row) {
      const refundId = row.refundId;
      this.$modal
        .confirm("是否确认同意退货？")
        .then(function () {
          return updateRefund({
            refundId: refundId,
            status: "2",
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {});
    },
  },
};
</script>
